import Login from './components/MyLogin.vue'
import Home from './components/MyHome.vue'
import Users from './components/menus/MyUsers.vue'
import Rights from './components/menus/MyRights.vue'
import Goods from './components/menus/MyGoods.vue'
import Orders from './components/menus/MyOrders.vue'
import Settings from './components/menus/MySettings.vue'
import UserDetail from './components/user/MyUserDetail.vue'

// 1.按需导入对应的函数
import {createRouter,createWebHashHistory} from 'vue-router'
// 2.创建路由对象
const router=createRouter({
    history:createWebHashHistory(),
    routes:[
        // 路由重定向
        {path:'/',redirect:'/login'},
        {path:'login',component:Login},
        // Home组件的路由规则
        {
            path:'/home',
            component:Home,
            // 用户访问/home时，重定向到/home/users
            redirect:'/home/users',
            // 子路由规则
            children:[
                {path:'users',component:Users},
                {path:'rights',component:Rights},
                {path:'goods',component:Goods},
                {path:'orders',component:Orders},
                {path:'settings',component:Settings},
                // 用户详情页的路由规则,并且开启props传参
                {path:'users/:id',component:UserDetail,props:true},
            ],
        },
    ],
})
// 全局路由导航守位
router.beforeEach((to,from,next)=>{
    // 如果用户访问的是登录页面，直接放行
    if(to.path==='/login'){
        return next();
    }
    // 获取Token值
    const token=localStorage.getItem('token');
    if(!token){
        // Token值不存在，强制跳转到登录页面
        return next('/login');
    }
    // 存在token值，直接放行
    next();
})
// 3.向外共享路由实例对象
export default router;